<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <div v-if="type == '1'">
        <el-form-item label="分组" prop="group_id">
          <el-select v-model="form.group_id" placeholder="请选择分组">
            <el-option
              v-for="item in group"
              :key="item.id"
              :label="item.group_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="小区" prop="community_id">
          <el-select v-model="form.community_id" placeholder="请选择小区">
            <el-option
              v-for="item in community"
              :key="item.id"
              :label="item.community_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="门牌号" prop="room_no">
          <el-input v-model="form.room_no" placeholder="请输入门牌号" />
        </el-form-item>
      </div>
      <div v-else-if="type == '2'">
        <el-form-item label="分组">
          <el-input v-model="form.group_name" disabled />
        </el-form-item>
        <el-form-item label="小区">
          <el-input v-model="form.community_name" disabled />
        </el-form-item>
        <el-form-item label="房间号">
          <el-input v-model="form.room_no" placeholder="请输入房间号">
            <template slot="prepend">{{ form.home_name }}</template>
          </el-input>
        </el-form-item>
      </div>
      <div v-else-if="type == '3'">
        <el-form-item label="分组">
          <el-input v-model="form.group_name" disabled />
        </el-form-item>
        <el-form-item label="房间信息">
          <el-input v-model="form.room_no" placeholder="请输入房间号">
            <template slot="prepend">{{ form.building_name }}</template>
          </el-input>
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { editHouse } from '@/api/room'
  import { getgroup } from '@/api/group'
  import { getCommunityList } from '@/api/community'

  export default {
    name: 'RoomInfo',
    data() {
      return {
        type: '1',
        form: {},
        rules: {
          group_id: [
            { required: true, trigger: 'blur', message: '请选择分组' },
          ],
          community_id: [
            { required: true, trigger: 'blur', message: '请选择小区' },
          ],
          room_no: [
            { required: true, trigger: 'blur', message: '请输入门牌号' },
          ],
        },
        group: [],
        community: [],
        title: '编辑',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(type, row) {
        this.type = type
        this.form = Object.assign({}, row)
        this.getGroup()
        this.getCommunity()
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.edit()
          }
        })
      },
      async getGroup() {
        const { data } = await getgroup({})
        this.group = data
      },
      async getCommunity() {
        const { data } = await getCommunityList({})
        this.community = data
      },
      async edit() {
        var formData = {
          room_id: this.form.id,
          room_no: this.form.room_no,
        }
        if (this.type == '1') {
          formData.group_id = this.form.group_id
          formData.community_id = this.form.community_id
        }
        const { data } = await editHouse(formData)
        if (data.is_success) {
          this.$baseMessage('修改成功', 'success', 'vab-hey-message-success')
          this.$emit('fetch-data')
          this.close()
        }
      },
    },
  }
</script>
<style lang="scss" scoped>
  .el-input.el-input--small {
    width: 215px;
  }
</style>
